home *** CD-ROM | disk | FTP | other *** search
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.TextField;
- import javax.microedition.rms.RecordStore;
-
- public class LogonForm implements CommandListener, ChessDisplayable, ServletResponseReceiver {
- private String servleturl = null;
- private String chessengineurl = null;
- private TextField username = null;
- private TextField password = null;
- private boolean responsereceived = false;
- private String response = null;
- private ChessLogic chesslogic = null;
- private ChessDisplayable chessreturn = null;
- private Form form;
- private Command OKCommand;
- private Command backCommand;
-
- public LogonForm(ChessLogic var1, ChessDisplayable var2) {
- this.chesslogic = var1;
- this.chessreturn = var2;
- this.loadURLs();
- this.initialize();
- }
-
- public void commandAction(Command var1, Displayable var2) {
- if (var1 == this.OKCommand) {
- MessageForm var3;
- if (this.logon()) {
- var3 = new MessageForm("Logon OK", CU.removeFirstTokens(this.response, 1), this.chessreturn);
- } else {
- var3 = new MessageForm("Logon failed", CU.removeFirstTokens(this.response, 1), this.chessreturn);
- }
-
- var3.makeCurrent(CU.getDisplay());
- }
-
- if (var1 == this.backCommand) {
- this.chessreturn.makeCurrent(CU.getDisplay());
- }
-
- }
-
- private void initialize() {
- this.OKCommand = new Command("OK", 4, 1);
- this.backCommand = new Command("Back", 2, 1);
- this.form = new Form("Logon to");
-
- String var1;
- String var2;
- try {
- Object var3 = null;
- RecordStore var5 = RecordStore.openRecordStore("Chess", false);
- if (var5.getRecord(3) == null) {
- var1 = "";
- } else {
- var1 = new String(var5.getRecord(3));
- }
-
- if (var5.getRecord(4) == null) {
- var2 = "";
- } else {
- var2 = new String(var5.getRecord(4));
- }
-
- var5.closeRecordStore();
- } catch (Exception var4) {
- CU.shout("RS user,pass load error: " + ((Throwable)var4).getMessage());
- var1 = "";
- var2 = "";
- }
-
- this.form.append("Please provide username and password \n(A password is only required for a registered logon)");
- this.username = new TextField("User-name: ", var1, 20, 0);
- this.password = new TextField("Password: ", var2, 20, 0);
- this.form.append(this.username);
- this.form.append(this.password);
- this.form.addCommand(this.OKCommand);
- this.form.addCommand(this.backCommand);
- }
-
- private void loadURLs() {
- try {
- Object var1 = null;
- RecordStore var3 = RecordStore.openRecordStore("Chess", false);
- this.servleturl = new String(var3.getRecord(1));
- this.chessengineurl = new String(var3.getRecord(2));
- var3.closeRecordStore();
- } catch (Exception var2) {
- CU.shout("RS load: " + ((Throwable)var2).getMessage());
- this.servleturl = "http://localhost:8080/requestservlet";
- this.chessengineurl = "chess.unix-ag.uni-kl.de";
- }
-
- }
-
- private boolean logon() {
- String var1 = this.username.getString();
- String var2 = this.password.getString();
-
- try {
- Object var3 = null;
- RecordStore var7 = RecordStore.openRecordStore("Chess", false);
- var7.setRecord(3, var1.getBytes(), 0, var1.length());
- var7.setRecord(4, var2.getBytes(), 0, var2.length());
- var7.closeRecordStore();
- } catch (Exception var6) {
- CU.shout("Store UP exception: " + ((Throwable)var6).getMessage());
- }
-
- CU.setUserName(var1);
- if (var1.length() < 3) {
- this.response = "logon Username must contain at least three characters";
- return false;
- } else {
- this.form.setTitle("Performing logon");
- ChessGame var8 = new ChessGame(this.chesslogic, this.servleturl);
- var8.setServletRedirection(this);
- if (!var2.equals("")) {
- var8.doPost("logon " + this.chessengineurl + " registered " + var1 + " " + var2, false);
- } else {
- var8.doPost("logon " + this.chessengineurl + " unregistered " + var1, false);
- }
-
- int var4;
- for(var4 = 0; !this.responsereceived && var4 < 300; ++var4) {
- try {
- Thread.sleep(500L);
- } catch (Exception var5) {
- }
- }
-
- if (var4 > 295) {
- this.response = "logon Logon timed out after 2 and a half minutes, please try again later";
- return false;
- } else if (CU.containsExpr(this.response, "logon Logon failed")) {
- this.chesslogic.setChessGame((ChessGame)null);
- return false;
- } else {
- this.chesslogic.setChessGame(var8);
- var8.setServletRedirection(this.chesslogic);
- return true;
- }
- }
- }
-
- public void makeCurrent(Display var1) {
- var1.setCurrent(this.form);
- this.form.setCommandListener(this);
- }
-
- public void receiveServletResponse(String var1) {
- this.response = var1;
- this.responsereceived = true;
- }
- }
-